
// 一. 导入koa
const Koa = require('koa')
// 二. 实例化对象
const app = new Koa()
// 三. 导入koa-router, 实例化路由对象
const Router = require('koa-router')
const router = new Router()

const crypto = require('crypto')
router.get('/users', (ctx) => {
  ctx.body = '这是用户页'
})
router.post('/users', (ctx) => {
  ctx.body = '创建用户页'
})


router.get('/',function (ctx){
    let {signature = '', timestamp = '', nonce = '', echostr = ''} = ctx.query
    console.log(signature, timestamp, nonce, echostr)
    let wx_token = "xxxxxxxxxxxxxx"
    let str = [wx_token, timestamp, nonce].sort().join('')
    let sha1 = crypto.createHash('sha1').update(str).digest('hex')
    if (sha1 !== signature) {
        ctx.body = 'token验证失败'
        return
      } else {
        ctx.body = echostr
      }
})

// 四. 注册路由中间件
// userRouter.routes() 加载路由规则
// userRouter.allowedMethods() 对于没有实现和没有使用的请求方式做出正确的响应
app.use(router.routes())
app.use(router.allowedMethods())
// 五. 启动服务
app.listen(3000, () => {
  console.log('server is running on http://localhost:3000')
})


